#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexTrigonometric(L"(-)?(sin|cos|tan|cot|sec|csc|tg)?\\d+(\\.\\d+)?°");

const map<string, string> mcMapStrStr_Trigonometric = {
	{"sin", "sine"},
	{"cos", "cosine"},
	{"tan", "tangent"},		{"tg", "tangent"},
	{"cot", "cotangent"},
	{"sec", "secant"},
	{"csc", "cosecant"}
};


void TextNormalizer::TnReTrigonometric(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while(regex_search(*fpInWsentence, tWsmatch, mcWregexTrigonometric))
	{
		string tInSentence = wstring2utf8string(*fpInWsentence);
		wstring tWstrMatch = tWsmatch[0];	//匹配到三角函数的字符串

		string tStrSign;
		if(tWstrMatch.length() > 0 && tWstrMatch[0] == L'-') {
			tStrSign = (fcInLanguage==LANG_EN) ? "negative" : "负";
			tWstrMatch = tWstrMatch.substr(1);		//去首部符号
		}

		string tStrUnit;
		if(tWstrMatch.length() > 1 && tWstrMatch[tWstrMatch.length()-1] == L'°') {
			tStrUnit = (fcInLanguage==LANG_EN) ? "degree" : "度";
			tWstrMatch = tWstrMatch.substr(0, tWstrMatch.length()-1);	//去尾部符号
		}

		wstring tWstrLetters = L"";
		wstring tWstrNumbers = L"";

		for(const wchar_t tcWchar: tWstrMatch) {
			if( IsWcharEnLetter(tcWchar) ) {
				tWstrLetters += tcWchar;
			}
			else if( IsWcharNumber(tcWchar) || tcWchar == L'.' ) {
				tWstrNumbers += tcWchar;
			}
		}

		string tStrTrigonometric;
		string tStrDegrees;

		auto tIterator = mcMapStrStr_Trigonometric.find( wstring2utf8string(tWstrLetters) );
		if(mcMapStrStr_Trigonometric.end() != tIterator) {
			tStrTrigonometric = tIterator->second;
		}

		tStrDegrees = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumbers)}, (fcInLanguage==LANG_EN)?RME_en_negative_decimal:RME_zh_negative_decimal);

		string tStrReplace = tStrSign + " " + tStrTrigonometric + " " + tStrDegrees + " " + tStrUnit;

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
